package models

import (
	"github.com/beego/beego/v2/client/orm"
)

// Cleaner保洁人员模型
type Cleaner struct {
	BaseModel
	Name      string `orm:"size(100)" json:"name"`        // 姓名
	Phone     string `orm:"size(20);unique" json:"phone"` // 电话
	Status    int8   `orm:"default(1)" json:"status"`     // 状态 1:在职 2:离职
	StoreIDs  string `orm:"type(text)" json:"store_ids"`  // 所属门店ID列表, JSON格式
	EntryDate int64  `json:"entry_date"`                  // 入职日期 (Unix时间戳)
}

// TableName 设置表名
func (c *Cleaner) TableName() string {
	return "ss_cleaner"
}

// AddCleaner 添加保洁人员
func AddCleaner(c *Cleaner) (int64, error) {
	o := orm.NewOrm()
	id, err := o.Insert(c)
	return id, err
}

// GetCleanerById 根据ID获取保洁人员
func GetCleanerById(id int64) (*Cleaner, error) {
	o := orm.NewOrm()
	cleaner := &Cleaner{BaseModel: BaseModel{Id: id}}
	err := o.Read(cleaner)
	return cleaner, err
}

// GetCleanerList 获取保洁人员列表
func GetCleanerList(page, pageSize int, filters map[string]interface{}) ([]*Cleaner, int64, error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Cleaner))

	// 处理筛选条件
	for k, v := range filters {
		qs = qs.Filter(k, v)
	}

	total, err := qs.Count()
	if err != nil {
		return nil, 0, err
	}

	var cleaners []*Cleaner
	_, err = qs.OrderBy("-created_at").Limit(pageSize, (page-1)*pageSize).All(&cleaners)

	return cleaners, total, err
}

// UpdateCleaner 更新保洁人员信息
func UpdateCleaner(c *Cleaner) error {
	o := orm.NewOrm()
	_, err := o.Update(c)
	return err
}

// DeleteCleaner 删除保洁人员
func DeleteCleaner(id int64) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(new(Cleaner)).Filter("id", id).Delete()
	return err
}
